package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public void add(Integer[] checkGroupIds, Setmeal setmeal) {

        //新增套餐，必须添加主键回显
        setmealMapper.add(setmeal);

        //在中间表中添加套餐和检查组的关联信息
        setSetmealAndCheckGroup(checkGroupIds,setmeal.getId());

        //将保存到数据库中的图片存入到redis的set集合中
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

        //删除缓存数据
        jedisPool.getResource().del(RedisMessageConstant.SETMEAL_LIST);
    }

    private void setSetmealAndCheckGroup(Integer[] checkGroupIds, Integer id) {
        
        if (checkGroupIds !=null&&checkGroupIds.length>0){
           setmealMapper.setSetmealAndCheckGroup(checkGroupIds,id);
        }
        
    }

    /**
     * 执行套餐分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage,pageSize);

        //条件查询套餐集合
        List<Setmeal> list = setmealMapper.findByCondition(queryString);

        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查询所有套餐列表
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        return setmealMapper.findAll();
    }

    /**
     * 通过id查询套餐、检查组和检查项
     */
   /* @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }*/

    @Override
    public Setmeal findById(Integer id) {

        Setmeal setmeal = setmealMapper.findById(id);
        //通过套餐的id查询检查组的信息
        List<CheckGroup> checkgroups = setmealMapper.findCheckGroupBySetmealId(setmeal.getId());

        //通过检查组的id查询检查项
        for (CheckGroup checkgroup : checkgroups) {
            List<CheckItem> checkItems = checkItemMapper.findByCheckGroupId(checkgroup.getId());
            checkgroup.setCheckItems(checkItems);
        }
        //将查询到的检查组封装到套餐中
        setmeal.setCheckGroups(checkgroups);
        return setmeal;
    }
}
